﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using Adartse.Sharepoint.Interfaces;

namespace Adartse.Sharepoint.WebPartEditors
{
    public class ShowWebPartEditor: EditorPart
    {
        private CheckBox _chkShowable;
        private TextBox _tbxParameter;
        private TextBox _tbxParameterValue;

        public ShowWebPartEditor()
        {
            this.Title = "Showing WebPart Configuration";
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            IShowableWebPart wp = (IShowableWebPart)this.WebPartToEdit;
            wp.EnableShow = _chkShowable.Checked;
            if (wp.EnableShow)
            {
                wp.ShowQueryStringParameter = _tbxParameter.Text;
                wp.ShowQueryStringParameterValue = _tbxParameterValue.Text;
            }

            return true;
        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();
            IShowableWebPart wp = (IShowableWebPart)this.WebPartToEdit;

            _chkShowable.Checked = wp.EnableShow;
            _tbxParameter.Text = wp.ShowQueryStringParameter;
            _tbxParameterValue.Text = wp.ShowQueryStringParameterValue;

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _chkShowable = new CheckBox();
            _tbxParameterValue = new TextBox();
            _tbxParameter = new TextBox();
        }

        protected override void CreateChildControls()
        {
            _chkShowable.Text = "Enable Conditional Show";
            _chkShowable.EnableViewState = true;
            _chkShowable.ID = "chkShowable";
            _chkShowable.Font.Bold = true;

            _tbxParameter.Width = new Unit(100, UnitType.Percentage);
            _tbxParameter.EnableViewState = true;
            _tbxParameter.ID = "tbxParameter";

            _tbxParameterValue.Width = new Unit(100, UnitType.Percentage);
            _tbxParameterValue.EnableViewState = true;
            _tbxParameterValue.ID = "tbxParameterValue";

            Controls.Add(_tbxParameterValue);
            Controls.Add(_tbxParameter);
            Controls.Add(_chkShowable);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            _chkShowable.RenderControl(writer);
            writer.WriteBreak();
            
            writer.RenderBeginTag(HtmlTextWriterTag.Strong);
            writer.Write("QueryString Parameter");
            writer.RenderEndTag();
            writer.WriteBreak();

            _tbxParameter.RenderControl(writer);
            writer.WriteBreak();

            writer.RenderBeginTag(HtmlTextWriterTag.Strong);
            writer.Write("Parameter Value");
            writer.RenderEndTag();
            writer.WriteBreak();

            _tbxParameterValue.RenderControl(writer);
        }
    }
}
